/**
 * 接口：菜单
 * @Author: 
 * @Date: 
 * @Copyright：
 */

import { postRequest, getRequest } from '/@/api/config/axios-fun';
import { ApiUtil } from "/@/utils/api";
import { useUserStore } from '/@/store/modules/system/user';
import { MENU_TYPE } from '../../constants/types';
import { setChildMenu } from "/@/utils/menu";

export const MenuAPI = {
    init: () => ({
        id: 0,
        appId: null,
        code: null,
        name: null,
        module: null,
        url: null,
        type: null,
        parentId: null,
        icon: null,
        parentFlag: null,
        displayFlag: null,
        sort: null,
    }),
    getMenu(params) {
        return new Promise((resolve) => {
            getRequest('menu/getMenu', params).then(({ res, data }) => {
                resolve({ ...ApiUtil.cleanGet({ res, data }) });
            });
        });
    },

    listMenu(params) {
        return new Promise((resolve) => {
            if (!params) {
                params = { appId: useUserStore().getAppInfo.id };
            } else if (!Object.prototype.hasOwnProperty.call(params, 'appId')) {
                params.appId = useUserStore().getAppInfo.id;
            }
            getRequest('menu/listMenu', params).then(({ res, data }) => {
                let list = [],
                    pageIndex = 1,
                    count = 0;
                if (data.code === 1) {
                    list = data.data.data;
                    list.forEach((element) => {
                        element.loading = false;
                        element.children = [];
                    });
                    if (data.data) {
                        pageIndex = data.data.currentPage;
                        count = data.data.count;
                    }
                }
                resolve({ res, data, list, pageIndex, count });
            });
        });
    },
    insertMenu(params, showMsgFlag = true, msg = '新增') {
        return new Promise((resolve) => {
            postRequest('menu/insertMenu', params).then(({ res, data }) => {
                resolve({ ...ApiUtil.cleanGet({ res, data }, showMsgFlag, msg) });
            });
        });
    },

    updateMenu(params, showMsgFlag = true, msg = '修改') {
        return new Promise((resolve) => {
            postRequest('menu/updateMenu', params).then(({ res, data }) => {
                resolve({ ...ApiUtil.cleanGet({ res, data }, showMsgFlag, msg) });
            });
        });
    },

    deleteMenu(params, showMsgFlag = true, msg = '删除') {
        return new Promise((resolve) => {
            postRequest('menu/deleteMenu', params, { inDataFlag: false }).then(({ res, data }) => {
                resolve({ ...ApiUtil.cleanGet({ res, data }, showMsgFlag, msg) });
            });
        });
    },

    /** 根据用户获取菜单 **/
    listMenuByUserId(params) {
        return new Promise((resolve) => {
            if (!params) {
                params = { appId: useUserStore().getAppInfo.id };
            } else if (!Object.prototype.hasOwnProperty.call(params, 'appId')) {
                params.appId = useUserStore().getAppInfo.id;
            }
            getRequest('menu/listMenuByUserId', params).then(({ res, data }) => {
                console.log(res, data);
                let list = [],
                    parentMenuList = [],
                    childMenuList = [],
                    boardMenuList = [];
                // eslint-disable-next-line prefer-const
                let { allMenu, parentMenu } = data.data;

                console.log(allMenu, parentMenu, "8889");
                parentMenu = (parentMenu || []).filter((menu) => menu.displayFlag);
                allMenu = (allMenu || []).filter((menu) => menu.displayFlag);
                const childMenu = [],
                    boardMenu = [];
                allMenu.forEach((menu) => {
                    menu.menuId = menu.id;
                    menu.menuName = menu.name;

                    if (menu.parentId !== 0) {
                        childMenu.push(menu);
                    }
                    if (menu.type === MENU_TYPE.BOARD.value) {
                        boardMenu.push(menu);
                    }
                });
                parentMenuList = JSON.parse(JSON.stringify(parentMenu));
                childMenuList = JSON.parse(JSON.stringify(childMenu));
                boardMenuList = JSON.parse(JSON.stringify(boardMenu));
                (parentMenu || []).forEach((menu) => {
                    menu.menuId = menu.id;
                    menu.menuName = menu.name;
                    menu.label = menu.name;
                    menu.value = menu.id;
                    menu.path = menu.url;
                    menu.parent = null;
                    setChildMenu(menu, childMenu);
                });
                list = parentMenu;
                resolve({ res, data, list, parentMenuList, childMenuList, boardMenuList, allMenu });
            });
        });
    },

    /** 根据用户获取包含权限按钮的菜单 **/
    listMenuWithOperationByUserId(params) {
        return new Promise((resolve) => {
            if (!params) {
                params = { appId: useUserStore().getAppInfo.id };
            } else if (!Object.prototype.hasOwnProperty.call(params, 'appId')) {
                params.appId = useUserStore().getAppInfo.id;
            }
            getRequest('menu/listMenuWithOperationByUserId', params).then(({ res = {}, data }) => {
                let childMenuList = [];
                if (data.code === 1) {
                    const { allMenu } = data.data;
                    //筛选所有子菜单和首页菜单
                    childMenuList = (allMenu || []).filter((menu) => menu.displayFlag || menu.url === '/');
                }
                resolve({ res, data, childMenuList });
            });
        });
    },

    /** 获取分级之后的菜单 **/
    listMenuWithChildren(params) {
        return new Promise((resolve) => {
            if (!params) {
                params = { appId: useUserStore().getAppInfo.id };
            } else if (!Object.prototype.hasOwnProperty.call(params, 'appId')) {
                params.appId = useUserStore().getAppInfo.id;
            }
            getRequest('menu/listMenuWithChildren', params).then(({ res, data }) => {
                let list = [];
                if (ApiUtil.isResponseSuccess(data)) {
                    list = data.data.data;
                }
                resolve({ res, data, list });
            });
        });
    },
};
